#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

int n, m, i, j, k, ans;
int tu[220][220];
int fa[220];
bool debug = false;
bool init() {
    int a, b, v;
    if (scanf("%d%d", &n, &m)==EOF) return false;
    memset(tu, 0, sizeof(tu));
    for (i=0; i<n; ++i) {
        scanf("%d%d%d", &a, &b, &v);
        tu[a][b] += v;
    }    
    return true;
}   


int bfs() {
    int a, b, minv; 
    queue<int> q;
    memset(fa, 0, sizeof(fa));
    fa[1] = -1;
    q.push(1);
    while (!q.empty()) {
        a = q.front();
        if (debug) printf("pop %d\n", a);
        q.pop();
        if (a==m) break;
        for (i=1; i<=m; ++i) 
            if (tu[a][i] > 0 && fa[i]==0) {
                fa[i] = a;
                q.push(i);
                if (debug) printf("push %d\n", i);
            }   
    }   

    if (a==m) {
        minv = tu[fa[m]][m];
        while (fa[a]!=-1) {
            if (minv > tu[fa[a]][a]) minv = tu[fa[a]][a];
            a = fa[a];
        }
        // Ôö¼Ó·´Ïò±ß 
        a = m;
        while (fa[a]!=-1) {
            tu[fa[a]][a] -= minv;
            tu[a][fa[a]] += minv;
            a = fa[a];
        }    
        if (debug) printf("Find way: %d\n", minv);
        return minv;
    }    
    return 0;
}    
 
void deal() {
    int v;
    ans = 0;
    while (1) {
        v = bfs();
        ans += v;
        if (v==0) break;
    }    
    printf("%d\n", ans);
}    
int main()
{
    while (init()) {
        deal();
    }        
    return 0;    
} 
